package com.example.yyjc.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.yyjc.domain.User;
import com.example.yyjc.domain.countryanalyze.CountryAnalysisReport;
import com.example.yyjc.domain.industryanalyze.IndustryAnalysisReport;
import com.example.yyjc.domain.selectionanalyze.SelectionAnalysisReport;
import com.example.yyjc.service.ReportService;
import com.example.yyjc.util.ResultBean;
import com.example.yyjc.util.excel.ExcelUtils;
import com.example.yyjc.util.token.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ReportController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-12-3 16:54
 **/
@RestController
@CrossOrigin
@RequestMapping("/report")
public class ReportController {

    Logger logger = LoggerFactory.getLogger(ReportController.class);

    @Autowired
    private ReportService reportService;

    @Autowired
    private ExcelUtils excelUtils;

    /**
     * 获取国家分析实验报告   (教师管理员查看全部，学生查看自己)
     * @param request
     * @return
     */
    @PostMapping("/getCountryAnalysisReport")
    public ResultBean getReport(HttpServletRequest request,@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String username =jsonObject.getString("username");
        if(StringUtils.isEmpty(pageNum) || pageNum == 0){
            pageNum = 1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize == 0){
            pageSize = 10;
        }
        /**
         * 获取用户信息
         */
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = TokenUtils.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token失效,请重新登录");
        }
        try{
            PageHelper.startPage(pageNum,pageSize);
            /**
             * 判断是否是教师或管理员
             */
            if(u.getRoleId() == 1 || u.getRoleId() == 2){
                List<CountryAnalysisReport> countryAnalysisReportList = reportService.getCountryAnalysisReports(null,null,username);
                PageInfo<CountryAnalysisReport> pageInfo = new PageInfo<CountryAnalysisReport>(countryAnalysisReportList);
                return ResultBean.success("查询成功",pageInfo);
            }else{
                List<CountryAnalysisReport> countryAnalysisReportList = reportService.getCountryAnalysisReports(u.getId(),null,username);
                PageInfo<CountryAnalysisReport> pageInfo = new PageInfo<CountryAnalysisReport>(countryAnalysisReportList);
                return ResultBean.success("查询成功",pageInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error(new Date() +"id是:" + u.getId() + "名字是:" + u.getUserName() + "查询实验报告失败");
            return ResultBean.error("查询实验报告信息错误。");
        }
    }

    /**
     * 添加国家分析的实验报告
     * @param countryAnalysisReport
     * @param request
     * @return
     */
    @PostMapping("/insertCountryAnalysisReport")
    public ResultBean insertCountryAnalysisReport(@RequestBody CountryAnalysisReport countryAnalysisReport,HttpServletRequest request){
        /**
         * 获取用户信息
         */
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = TokenUtils.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token失效,请重新登录");
        }
        //添加提交的用户id
        countryAnalysisReport.setUserId(u.getId());
        try{
            reportService.insertCountryAnalysisReport(countryAnalysisReport);
            return ResultBean.success("插入实验报告成功");
        }catch (Exception e){
            logger.error(u.getUserName() + "插入国家分析实验报告失败{}",new Date());
            return ResultBean.error("实验报告插入失败");
        }
    }

    /**
     * 删除国家分析的实验报告
     * @return
     */
    @PostMapping("/deleteCountryAnalysisReport")
    public ResultBean deleteCountryAnalysisReport(@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Long countryAnalysisId = jsonObject.getLong("countryAnalysisId");
        try{
            reportService.deleteCountryAnalysisReport(countryAnalysisId);
            return ResultBean.success("删除实验报告成功");
        }catch (Exception e){
            logger.error("删除国家分析实验报告失败{}",new Date());
            return ResultBean.error("实验报告删除失败");
        }
    }

    /**
     * 获取行业分析实验报告   (教师管理员查看全部，学生查看自己)
     * @param request
     * @return
     */
    @PostMapping("/getIndustryAnalysisReport")
    public ResultBean getIndustryAnalysisReport(HttpServletRequest request,@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String username = jsonObject.getString("username");
        if(StringUtils.isEmpty(pageNum) || pageNum == 0){
            pageNum = 1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize == 0){
            pageSize = 10;
        }
        /**
         * 获取用户信息
         */
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = TokenUtils.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token失效,请重新登录");
        }
        try{
            PageHelper.startPage(pageNum,pageSize);
            /**
             * 判断是否是教师或管理员
             */
            if(u.getRoleId() == 1 || u.getRoleId() == 2){
                List<IndustryAnalysisReport> industryAnalysisReports = reportService.getIndustryAnalysisReports(null,null,username);
                PageInfo<IndustryAnalysisReport> pageInfo = new PageInfo<IndustryAnalysisReport>(industryAnalysisReports);
                return ResultBean.success("查询成功",pageInfo);
            }else{
                List<IndustryAnalysisReport> industryAnalysisReports = reportService.getIndustryAnalysisReports(u.getId(),null,username);
                PageInfo<IndustryAnalysisReport> pageInfo = new PageInfo<IndustryAnalysisReport>(industryAnalysisReports);
                return ResultBean.success("查询成功",pageInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error(new Date() +"id是:" + u.getId() + "名字是:" + u.getUserName() + "查询实验报告失败");
            return ResultBean.error("查询实验报告信息错误。");
        }
    }

    /**
     * 添加行业分析的实验报告
     * @return
     */
    @PostMapping("/insertIndustryAnalysisReport")
    public ResultBean insertIndustryAnalysisReport(@RequestBody IndustryAnalysisReport industryAnalysisReport,HttpServletRequest request){
        /**
         * 获取用户信息
         */
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = TokenUtils.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token失效,请重新登录");
        }
        //添加提交的用户id
        industryAnalysisReport.setUserId(u.getId());
        try{
            reportService.insertIndustryAnalysis(industryAnalysisReport);
            return ResultBean.success("插入实验报告成功");
        }catch (Exception e){
            logger.error(u.getUserName() + "插入行业分析实验报告失败{}",new Date());
            return ResultBean.error("实验报告插入失败");
        }
    }

    /**
     * 删除行业分析的实验报告
     * @return
     */
    @PostMapping("/deleteIndustryAnalysisReport")
    public ResultBean deleteIndustryAnalysisReport(@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Long industryAnalysisId = jsonObject.getLong("industryAnalysisId");
        try{
            reportService.deleteIndustryAnalysisReport(industryAnalysisId);
            return ResultBean.success("删除实验报告成功");
        }catch (Exception e){
            logger.error("删除行业分析实验报告失败{}",new Date());
            return ResultBean.error("实验报告删除失败");
        }
    }

    /**
     * 获取选品分析实验报告   (教师管理员查看全部，学生查看自己)
     * @param request
     * @return
     */
    @PostMapping("/getSelectionAnalysisReport")
    public ResultBean getSelectionAnalysisReport(HttpServletRequest request,@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String username =jsonObject.getString("username");
        if(StringUtils.isEmpty(pageNum) || pageNum == 0){
            pageNum = 1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize == 0){
            pageSize = 10;
        }
        /**
         * 获取用户信息
         */
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = TokenUtils.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token失效,请重新登录");
        }
        try{
            PageHelper.startPage(pageNum,pageSize);
            /**
             * 判断是否是教师或管理员
             */
            if(u.getRoleId() == 1 || u.getRoleId() == 2){
                List<SelectionAnalysisReport> selectionAnalysisReports = reportService.getSelectionAnalysisReports(null,null,username);
                PageInfo<SelectionAnalysisReport> pageInfo = new PageInfo<SelectionAnalysisReport>(selectionAnalysisReports);
                return ResultBean.success("查询成功",pageInfo);
            }else{
                List<SelectionAnalysisReport> selectionAnalysisReports = reportService.getSelectionAnalysisReports(u.getId(),null,username);
                PageInfo<SelectionAnalysisReport> pageInfo = new PageInfo<SelectionAnalysisReport>(selectionAnalysisReports);
                return ResultBean.success("查询成功",pageInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error(new Date() +"id是:" + u.getId() + "名字是:" + u.getUserName() + "查询实验报告失败");
            return ResultBean.error("查询实验报告信息错误。");
        }
    }

    /**
     * 添加行业分析的实验报告
     * @return
     */
    @PostMapping("/insertSelectionAnalysisReport")
    public ResultBean insertSelectionAnalysisReport(@RequestBody SelectionAnalysisReport selectionAnalysisReport,HttpServletRequest request){
        /**
         * 获取用户信息
         */
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = TokenUtils.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token失效,请重新登录");
        }
        //添加提交的用户id
        selectionAnalysisReport.setUserId(u.getId());
        try{
            reportService.insertSelectionAnalysisReport(selectionAnalysisReport);
            return ResultBean.success("插入实验报告成功");
        }catch (Exception e){
            logger.error(u.getUserName() + "插入行业分析实验报告失败{}",new Date());
            return ResultBean.error("实验报告插入失败");
        }
    }

    /**
     * 删除行业分析的实验报告
     * @return
     */
    @PostMapping("/deleteSelectionAnalysisReport")
    public ResultBean deleteSelectionAnalysisReport(@RequestBody String data){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Long selectionAnalysisId = jsonObject.getLong("selectionAnalysisId");
        try{
            reportService.deleteSelectionAnalysisReport(selectionAnalysisId);
            return ResultBean.success("删除实验报告成功");
        }catch (Exception e){
            logger.error("删除行业分析实验报告失败{}",new Date());
            return ResultBean.error("实验报告删除失败");
        }
    }

    /**
     * 导出所有国家分析实验报告信息
     */
    @GetMapping("/importCountryAnalysisReport")
    public void importAllUser(HttpServletRequest request, HttpServletResponse response, String data){
        List<CountryAnalysisReport> countryAnalysisReports = reportService.getCountryAnalysisReports(null,data,null);
        excelUtils.cooperationData(request,response,countryAnalysisReports,"国家分析实验报告信息",CountryAnalysisReport.class);
    }

    /**
     * 导出所有行业分析实验报告信息
     */
    @GetMapping("/importIndustryAnalysisReport")
    public void importIndustryAnalysisReport(HttpServletRequest request, HttpServletResponse response, String data){
        List<IndustryAnalysisReport> industryAnalysisReports = reportService.getIndustryAnalysisReports(null,data,null);
        excelUtils.cooperationData(request,response,industryAnalysisReports,"行业分析实验报告信息",IndustryAnalysisReport.class);
    }

    /**
     * 导出所有选品分析实验报告信息
     */
    @GetMapping("/importSelectionAnalysisReport")
    public void importSelectionAnalysisReport(HttpServletRequest request, HttpServletResponse response, String data){
        List<SelectionAnalysisReport> selectionAnalysisReports = reportService.getSelectionAnalysisReports(null,data,null);
        excelUtils.cooperationData(request,response,selectionAnalysisReports,"选品分析实验报告信息",SelectionAnalysisReport.class);
    }
}